package com.study.system.service;

import com.study.system.entity.ManageClubLog;
import com.study.system.entity.ManageUserLog;
import com.study.system.entity.Post;
import com.study.system.mapper.PostMapper;
import com.study.system.vo.ClubInfoVO;
import com.study.system.entity.Club;
import com.study.system.exception.ClubExistException;
import com.study.system.mapper.ClubMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ClubService {

    @Resource
    private ClubMapper clubMapper;

    @Resource
    private PostMapper postMapper;

    /**
     * 根据用户ID查询所有参加的社团
     *
     * @param userId 用户ID
     * @return 查询结果
     */
    public List<ClubInfoVO> selectAllClubByUserId(Integer userId) {
        List<Integer> ids = clubMapper.selectAllClubIdByUserId(userId);
        List<ClubInfoVO> result = new ArrayList<>();
        for (Integer id : ids) {
            result.add(clubMapper.selectByClubId(id));
        }
        return result;
    }

    /**
     * 用户加入社团
     *
     * @param userId 用户id
     * @param clubId 社团id
     * @return 操作是否成功
     */
    public Boolean insertAddClubLog(Integer userId, Integer clubId) {
        // 查看用户是否已经加入该社团
        Integer result = clubMapper.insertAddClubLog(userId, clubId);
        return result != 0;
    }

    /**
     * 查询所有可以加入的社团
     *
     * @param userId 用户id
     * @return 查询结果
     */
    public List<ClubInfoVO> selectAllClubCouldAttend(Integer userId) {
        List<Integer> ids = clubMapper.selectAllClubIdByUserId(userId);
        if (ids.isEmpty()) {
            return clubMapper.selectAllClub();
        }
        List<Integer> targetIds = clubMapper.selectAllClubIdNotInList(ids);
        List<ClubInfoVO> result = new ArrayList<>();
        for (Integer id : targetIds) {
            result.add(clubMapper.selectByClubId(id));
        }
        return result;
    }

    /**
     * 插入社团信息
     *
     * @param creatorId 创建者ID
     * @param clubInfo  社团信息
     * @return 操作结果
     * @throws ClubExistException 社团重复异常
     */
    public Boolean insetClub(Integer creatorId, Club clubInfo) throws ClubExistException {
        Club clubDB = clubMapper.selectByClubName(clubInfo.getName());
        // 社团名称重复
        if (clubDB != null) {
            throw new ClubExistException();
        }
        clubInfo.setCreatorId(creatorId);
        Integer result = clubMapper.insertClubInfo(clubInfo);
        return result == 1;
    }

    /**
     * 查询所有社团信息
     *
     * @return 查询结果
     */
    public List<ClubInfoVO> selectAllClub() {
        return clubMapper.selectAllClub();
    }

    /**
     * 查询所有可以管理的社团信息
     *
     * @param userId 用户ID
     * @return 查询结果
     */
    public List<Club> selectAllManageClub(Integer userId) {
        return clubMapper.selectAllManageClub(userId);
    }

    /**
     * 用户退出社团
     *
     * @param userId 用户Id
     * @param clubId 社团Id
     * @return 操作是否成功
     */
    public Boolean userExitClub(Integer userId, Integer clubId) {
        return clubMapper.userExitClub(userId, clubId) == 1;
    }

    // 更新社团信息
    public Boolean updateClubInfo(Club clubInfo) {
        Integer clubId = clubInfo.getId();
        String name = clubInfo.getName();
        String profile = clubInfo.getProfile();
        String phone = clubInfo.getPhone();
        String rules = clubInfo.getRules();

        return clubMapper.updateClubInfo(clubId, name, profile, phone, rules) == 1;
    }

    public Boolean adminUpdateClubInfo(Club clubInfo) {
        Integer status = clubInfo.getStatus();
        Integer clubId = clubInfo.getId();
        String name = clubInfo.getName();
        String profile = clubInfo.getProfile();
        String phone = clubInfo.getPhone();
        String rules = clubInfo.getRules();

        return clubMapper.adminUpdateClubInfo(status, clubId, name, profile, phone, rules) == 1;
    }

    // 获取指定社团信息
    public ClubInfoVO getClubInfoById(ClubInfoVO clubInfoVO) {
        Integer clubId = clubInfoVO.getId();
        ClubInfoVO result = clubMapper.getInfoById(clubId);
        result.setId(clubInfoVO.getId());
        return result;
    }

    /**
     * 社团提出成员
     *
     * @param operatorId 操作者ID
     * @param clubId 社团ID
     * @param userId 用户ID
     * @return 操作是成功
     */
    public Boolean deleteMember(Integer operatorId, Integer clubId, Integer userId) {
        // 先获取当前用户的职位级别
        Post operatorPost = postMapper.getPostByUser(operatorId, clubId);
        Post currentPost = postMapper.getPostByUser(userId, clubId);
        if (operatorPost.getLevel() >= currentPost.getLevel()) {
            return false;
        }
        return clubMapper.deleteMember(clubId, userId) == 1;
    }

    public Integer getMemberNumber(Integer clubId) {
        return clubMapper.getMemberNumber(clubId);
    }

    public List<ClubInfoVO> selectAllClubLimit(Integer pageSize, Integer currentIndex) {
        return clubMapper.getAllClubLimit(pageSize, currentIndex);
    }

    public List<ManageClubLog> getAllManageClubLog() {
        return clubMapper.getAllManageClubLog();
    }

    public List<ManageUserLog> getAllManageUserLog() {
        return clubMapper.getAllManageUserLog();
    }

    public Integer getClubCount() {return clubMapper.getClubCount();}
}
